<template>
  <div class="app-container">
    <!--顶部搜索条件-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="监控名称" prop="monitorName">
        <el-input
          v-model="queryParams.monitorName"
          placeholder="请输入监控名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上报状态" prop="reportStatus">
        <el-select
          v-model="queryParams.reportStatus"
          placeholder="请选择上报状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.ygps_report_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="监控区域" prop="monitorArea">
        <el-select
          v-model="queryParams.monitorArea"
          placeholder="请选择监控区域"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.video_monitor_area"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型" prop="monitorType">
        <el-select
          v-model="queryParams.monitorType"
          placeholder="请选择设备类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.video_monitor_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--顶部操作按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['iot:storage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['iot:storage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['iot:storage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['iot:storage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--表格-->
    <el-table v-loading="loading" :data="monitorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="监控名称" prop="monitorName" align="center" width="150" />
      <el-table-column label="视频链接" prop="monitorUrl" align="center" width="450" :show-overflow-tooltip="true"/>
      <el-table-column label="设备类型" prop="monitorType" align="center" width="120">
        <template slot-scope="scope">
          <span>
            {{ selectDictLabels(dict.type.video_monitor_type,scope.row.monitorType) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="监控区域" prop="monitorArea" align="center" width="150">
        <template slot-scope="scope">
          <span>
            {{ selectDictLabels(dict.type.video_monitor_area,scope.row.monitorArea) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="上报状态" prop="reportStatus" align="center" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.reportStatus === '0'" type="success">已上报</el-tag>
          <el-tag v-else type="danger">未上报</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上报设备号" prop="reportTempDeviceNo" align="center" width="180">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.reportStatus === '0'" type="success">{{scope.row.reportTempDeviceNo}}</el-tag>
          <el-tag v-else type="danger">未上报</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding" fixed="right" min-width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['iot:storage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['iot:storage:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="监控名称" prop="monitorName">
              <el-input v-model="form.monitorName" placeholder="请输入监控名称" maxlength="32" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冷库类型"  prop="monitorType">
              <el-radio-group v-model="form.monitorType">
                <el-radio v-for="dict in dict.type.video_monitor_type" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="监控链接" prop="monitorUrl">
              <el-input v-model="form.monitorUrl" placeholder="请输入监控链接" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="冷库类型"  prop="monitorArea">
              <el-radio-group v-model="form.monitorArea">
                <el-radio v-for="dict in dict.type.video_monitor_area" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addMonitor, delMonitor, getMonitor, listMonitor, updateMonitor} from "@/api/video/monitor";

export default {
  name: "monitorIndex",
  dicts: ['video_monitor_area','ygps_report_status','video_monitor_type'],
  data(){
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        monitorName: undefined,
        reportStatus: undefined,
        monitorArea: undefined,
        monitorType: undefined
      },
      dateRange: [],
      showSearch: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      loading: false,
      monitorList: [],
      total: 0,
      title: "",
      open: false,
      form: {},
      rules: {
        monitorName: [
          { required: true, message: "监控名称不能为空", trigger: "blur" },
          { min: 2, max: 32, message: '监控名称长度必须介于 2 和 32 之间', trigger: 'blur' }
        ],
        monitorType: [
          { required: true, message: "监控类型不能为空", trigger: "blur" },
        ],
        monitorUrl: [
          { required: true, message: "监控链接不能为空", trigger: "blur" },
        ],
        monitorArea: [
          { required: true, message: "监控安装区域不能为空", trigger: "blur" },
        ],
      }

    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList(){
      this.loading = true
      listMonitor(this.addDateRange(this.queryParams,this.dateRange)).then(response => {
        this.monitorList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery(){
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery(){
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    reset(){
      this.form = {
        monitorName: undefined,
        monitorType: "1",
        monitorUrl: undefined,
        monitorArea: "0",
        remark: ""
      }
    },
    handleAdd(){
      this.reset()
      this.open = true
      this.title = "添加监控"
    },
    handleDelete(row){
      const ids = row.id || this.ids
      if (row.id && row.reportStatus === "0"){
        this.$modal.msgWarning("当前监控设备已上报，请先取消后再删除")
      }else {
        const message = row.id ? "是否删除当前数据项？" : "是否确认删除当前选中的数据项？"
        this.$modal.confirm(message).then(function() {
          return delMonitor(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        }).catch(() => {})
      }
    },
    handleUpdate(row){
      this.reset()
      const id = row.id || this.ids
      getMonitor(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改监控"
      })
    },
    handleExport(){
      this.$modal.msgError("开发中，敬请期待")
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    submitForm: function (){
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined){
            updateMonitor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          }else {
            addMonitor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    cancel(){
      this.open = false
      this.reset()
    }
  }
}
</script>

<style scoped>

</style>
